Handling Image Uploads

Pre-Reqs

Add a 'Files' Table to the Database

Take a look at the following SQL statement and then run it in PHPMyAdmin to create the table (in the my-new-site database):

DROP TABLE IF EXISTS files;
CREATE TABLE IF NOT EXISTS files (
  fileId int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  fileName varchar(200) NOT NULL,
  fileDescription varchar(200) NOT NULL,
  fileExtension varchar(10) NOT NULL,
  fileSize int(11) NOT NULL
) ENGINE=InnoDB;		
	

Note that we are going to use this table to keep track of all the images that we add to our blog posts. But it could potentially be used to attach other files to blog posts as well, which is why the table is called 'files' rather than 'images'.

Create a FileDataAccess Class

  1. Create a file in the 'includes' folder and name it FileDataAccess.inc.php.
  2. Paste this code into the file.
    Note the following about this code:
  3. Create a file inside the 'tests' folder called file-data-access-tests.php and add this code to it.
    This page will test the methods in the FileDataAccess class.
    The tests will not work right now because FileDataAccess class is not yet complete. But in the following steps you'll implement code for each method in the FileDataAccess class.
    When you finish writing the code for a method in the FileDataAccess class, you'll test it in this page.
    Make sure that you are not getting any errors, and confirm that the data returned by each method is correct.
  4. Start out by writing the code for insertFile() method in the FileDataAccess class.
    Refer to the PageDataAccess class for help, this method is similar to insertPage().
    Make sure to run the test page after adding the body of the insertFile() method.
  5. Implement the code for the getFileList() method in the FileDataAccess class.
    The method should fetch all the columns from the 'files' table.
    To help you, look at the similar method in the PageDataAcess class.
    Test the getFileList() method in the test page.
  6. Implement the code for the getFileById() method in the FileDataAccess class.
    For the getFileById() method, you should fetch all the columns from the 'files' table.
    To help you, look at the similar method in the PageDataAcess class.
    Test the getFileById() method in the test page.
  7. Implement the code for the updateFile() method in the FileDataAccess class. To help you, look at the similar method in the PageDataAcess class.
    Test the updateFile() method in the test page.

Create a File List Page

Setting Up the File Details Page

Eventually, this page will allow you to upload image files to your web server. But we won't worry about that yet. For now, we'll just focus on inserting and updating rows in the files table.

Finishing Up the File Details Page

Notice in the file-details.php page, that there are comments that mark off our next steps. To get started, find the comment that says STEP 1.

Final Notes

Follow Up Questions: